Fortran For Fun 并行之openmp

OpenMP是共享式内存并行库,编译器通常自带该库,编译时只需要添加相应的编译选项,

gfortran选项

1
-fopenmp

ifort 选项

1
-openmp

lean_omp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
program learn_omp
use omp_lib
implicit none
integer :: it, nt
!$omp parallel private(it)
it = omp_get_thread_num()
if(it==0) then
nt = omp_get_num_threads()
endif
print'(a,i0,a,i0)', 'hello openmp from thread ',it, ' with total threads ',nt
!$omp end parallel
end program learn_omp

结果

1
2
3
4
5
6
7
8
hello openmp from thread 6 with total threads 8
hello openmp from thread 7 with total threads 8
hello openmp from thread 5 with total threads 8
hello openmp from thread 4 with total threads 8
hello openmp from thread 3 with total threads 8
hello openmp from thread 0 with total threads 8
hello openmp from thread 1 with total threads 8
hello openmp from thread 2 with total threads 8

openmp